<?php
// -----------------------------------------------------------
// (1) 多主机连接配置
// -----------------------------------------------------------
$config['127.0.0.1(本机)'] = array('127.0.0.1', 'root', 'quanhz');


// -----------------------------------------------------------
// (2) 函数库
// -----------------------------------------------------------

// 错误消息
function error($message) {
	exit('<div style="font-size:14px; color:#f00; font-weight:bold; text-align:center;">'.$message.'</div>');
}

// 连接数据库
function connect($host) {
	global $config;
	
	isset($config[$host]) or error('Host no found');

	try {
		$conn = @mysql_connect($config[$host][0], $config[$host][1], $config[$host][2]) or error('用户名密码错误！');

	} catch(Exception $error) {
	}
	mysql_query('SET NAMES UTF8', $conn);
	
	return $conn;
}

// 显示主机
function showHosts() {
	global $config;
	$output = '';
	
	foreach ($config as $key => $row) {
		$output .= '<a href="/mysql/index.php/'.$key.'">'.$key.'</a><br />';	
	}
	
	return $output;
}

// 显示数据库
function showDatabases($host) {
	$conn = connect($host);
	$output = '';
	
	$res = mysql_query('SHOW DATABASES', $conn);
	while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
		$output .= '<a href="/mysql/index.php/'.$host.'/'.$row['Database'].'">'.$row['Database'].'</a><br>';
	}
	
	return $output;
}

// 显示表
function showTables($host, $db) {
	$conn = connect($host);
	$output = '';
	
	$resTable = mysql_query('SHOW TABLES FROM '.$db);
	while ($rowTable = mysql_fetch_array($resTable, MYSQL_ASSOC)) {
		$output .= '<a href="/mysql/index.php/'.$host.'/'.$db.'/'.current($rowTable).'">'.current($rowTable).'</a><br />';
	}
	
	return $output;
}

// 显示数据
function showData($host, $db, $table) {
	$conn = connect($host);
	$output = '';
	
	$resColumn = mysql_query('SHOW FULL COLUMNS FROM '.$db.'.'.$table);
	$output .= '<table cellspacing="0">';
	$th = '<tr>';
	while ($rowColumn = mysql_fetch_array($resColumn, MYSQL_ASSOC)) {
		$th .= '<th style="font-size:12px;" title="'.$rowColumn['Type'].'">'.$rowColumn['Comment'].'<div style="color:#999; ">'.$rowColumn['Field'].'</div></th>';
	}
	$th .= '</tr>';

	$limit = empty($_GET['limit']) ? '100' : $_GET['limit'];
	$resData = mysql_query('SELECT * FROM '.$db.'.'.$table.' LIMIT '.$limit);	

	$output .= $th;
	while ($rowData = mysql_fetch_array($resData, MYSQL_ASSOC)) {
		$output .= '<tr>';
		foreach ($rowData as $item) {
			$output .= '<td>'.htmlspecialchars($item).'</td>';
		}
		$output .= '</tr>';
	}
	$output .= mysql_num_rows($resData) > 10 ? $th : '';
	$output .= '</table>';
	$output .= mysql_num_rows($resData);
	
	return $output;
}

// 显示路径
function showPath() {
	global $host, $db, $table, $sql;
	$output = '';
	
	$output .= '<a href="/mysql/index.php">/</a> =》 ';
	$output .= '<a href="/mysql/index.php/'.$host.'">'.$host.'</a> =》 ';
	$output .= '<a href="/mysql/index.php/'.$host.'/'.$db.'">'.$db.'</a> =》 ';
	$output .= '<a href="/mysql/index.php/'.$host.'/'.$db.'/'.$table.'">'.$table.'</a> =》 ';
	$output .= '<br /><br />';
	$output .= '<form><textarea name="sql" style="width:80%;">'.$sql.'</textarea><input type="submit" value="Query" /></form>';

	return $output;
}


// -----------------------------------------------------------
// (3) 流程控制
// -----------------------------------------------------------
echo '
<title>MySQL浏览器</title><style>
	body { font-size:14px; line-height:20px; } 
	a { color:#00f; } 
	table { border-left:#ccc 1px solid; border-top:#ccc 1px solid; }
	th, td { border-right:#ccc 1px solid; border-bottom:#ccc 1px solid; padding:5px; }
	td { font-size:12px; color:#f00; }
	th { font-weight:normal; background:#eee; }
</style>';

// Route
$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
$pathInfo = explode('/', substr($path, 1));

$host = isset($pathInfo[0]) ? $pathInfo[0] : '';
$db = isset($pathInfo[1]) ? $pathInfo[1] : '';
$table = isset($pathInfo[2]) ? $pathInfo[2] : '';
$sql = isset($_GET['sql']) ? $_GET['sql'] : '';


$output = '';
$output .= showPath();
if ($host === '') {
	$output .= showHosts();
} elseif ($db === '') {
	$output .= showDatabases($host);
} elseif ($table === '') {
	$output .= showTables($host, $db);
} else {
	$output .= showData($host, $db, $table);
}
echo $output;